#!/bin/bash
# autopyfactory   Init script for running the autopyfactory
#
# Author:       Rod Walker
#
# chkconfig: - 98 02
#
# description: Panda pilot submission for ATLAS
# processname: factory.py

PATH=/usr/bin:/sbin:/bin:/usr/sbin
export PATH

lockfile=${LOCKFILE-/var/lock/subsys/autopyfactory}
pidfile=${PIDFILE-/var/run/autopyfactory/autopyfactory.pid}
RETVAL=0

# Source function library.
. /etc/rc.d/init.d/functions

autofactoryd=/opt/panda/autopyfactory/bin/factory.py
AUTOPYFACTORY_OPTS="--conf=/opt/panda/autopyfactory/var/base.conf"
AUTOPYFACTORY_EXTRA_OPTS="--log=/home/atlas/factory.log"

# Determine if we can use the -p option to killproc.  RHEL < 5 can't.
killproc | grep -q -- '-p' 2>/dev/null && killopts="-p $pidfile"

# Figure out if the system just booted. Let's assume
# boot doesn't take longer than 5 minutes
## Not used for now
##[ -n "$INIT_VERSION" ] && PUPPET_OPTS="${PUPPET_OPTS} --fullrun"

start() {
    echo -n $"Starting autopyfactory: "
    daemon --user atlas /usr/bin/python $autofactoryd ${AUTOPYFACTORY_OPTS} ${AUTOPYFACTORY_EXTRA_OPTS}
    RETVAL=$?
    echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

stop() {
    echo -n $"Stopping puppet: "
    killproc $killopts $puppetd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

restart() {
    stop
    start
}

rh_status() {
    status | grep -q -- '-p' 2>/dev/null && statusopts="-p $pidfile"
    status $statusopts $autopyfactoryd
    RETVAL=$?
    return $RETVAL
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    status)
        rh_status
    ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 1
esac

exit $RETVAL
